package com.guanyu.u2cache.confinuelife;

import com.guanyu.u2cache.cache.CacheOperate;
import com.guanyu.u2cache.constant.ExcuConfig;
import com.guanyu.u2cache.model.EventData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.DelayQueue;

/**
 * @author zhouyunjian
 * @date 2023/11/20 17:15
 **/
@Slf4j
@Component
public class CacheReload {

    @Resource
    private CacheOperate cacheOperate;

    @Async(ExcuConfig.name)
    public void reloadCache(CachedInvocation invocation, DelayQueue<DelayedTask> delayQueue) {
        EventData metaData = invocation.getMetaData();
        Object invoke = invocation.invoke();
        cacheOperate.putCache(metaData.getCacheNames(), metaData.getKey(), metaData.getCacheType(), invoke, metaData.getReturnType(), metaData.getExpiredTime(), metaData.getExpireUnit());
        log.debug("key[{}]缓存重载处理完成，结果[{}]", metaData.getKey(), invoke);
        delayQueue.offer(new DelayedTask(invocation));
    }
}
